<template>
    <el-dialog title="领劵规则" :visible.sync="open" :before-close="close" append-to-body>
        <CouRulsEdit />
        <el-row>
            <el-col :span="24">
                <el-button type="danger" icon="el-icon-plus" @click="newadd">添加抢购活动</el-button>
            </el-col>
        </el-row>
        <el-table class="table-data" :data="td" style="width: 100%;margin-top: 20px"
                v-loading="loading"
                element-loading-text="数据加载中..." >
                <el-table-column prop="num" label="参与人数" ></el-table-column>
                <el-table-column prop="couponId" label="优惠券ID" ></el-table-column>
                <el-table-column label="操作" >
                    <template slot-scope="scope">
                        <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
    </el-dialog>
</template>
<script>
import moment from 'moment'
import CouRulsEdit from 'components/Coupon/CouRulsEdit'
export default {
    data(){
        return{
            open:false,
            marketingId:-1,
            td:[],
            loading:false
        }
    },
    computed:{
        getstate(){
            return this.$store.state.couruleclass
        },
        getchange(){
            return this.$store.state.courule
        },
        getdialog(){
            return this.$store.state.courulesdialog
        }
    },
    watch:{
        getstate(v){//打开当前弹窗
            this.open=v
        },
        getchange(v){//获取修改信息
            if(v.marketingId){
                this.marketingId=v.marketingId
                this.getData()
            }
        },
        getdialog(v){//获取子弹窗的打开关闭情况
            if(!v){
                this.$store.state.courulesdialog=true
                this.getData()
            }
        }
    },
    components:{
        CouRulsEdit
    },
    methods:{
        close(){//关闭当前弹窗,清空数据
            this.td=[]
            this.marketingId=-1
            this.$store.state.couruleclass=false
            this.$store.state.courule={}
        },
        newadd(){//打开新增弹窗
            var obj={}
            obj.marketingId=this.marketingId
            this.$store.commit("ChangeCouRuleEdit",obj)
        },
        handleEdit(i,r){//带参数编辑
            this.$store.commit("ChangeCouRuleEdit",r)
        },
        getData(){//获取数据
            if(this.marketingId==-1||this.marketingId==undefined){
                return
            }
            var _this=this
            this.loading=true
            var pp=new URLSearchParams()
            pp.append('marketingId',this.marketingId)
            axios.get('/listShareCouponRel.do',{params:pp}).then(r=>{
                if(r.data.code==200){
                    _this.td=[].concat(r.data.response.shareCouponRelList)
                    _this.loading=false
                }else{
                    _this.$message.error(r.data.message)
                    _this.loading=false
                }
            })
        }
    }
}
</script>
